﻿package src.flip{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	import flash.printing.*; 
	import src.events.*;
	import src.utils.*;
	import src.caurina.transitions.*;
	public final class Printer extends Sprite {
		
		private var flip:Flip = Flip.flip;
		private var config:Object = Flip.config;
		//
		private var myPrintJob:PrintJob;
		public function Printer():void {
			this.visible = false;
			this.filters = Flip.getShadow();
			printer_head.addEventListener(MouseEvent.MOUSE_DOWN, headDownHandler);
			flip.stage.addEventListener(MouseEvent.MOUSE_UP, headUpHandler);
			printer_button.addEventListener(MouseEvent.CLICK, buttonHandler);
			
			flip.addEventListener(FlipEvent.RESIZE, resizeHandler);
			flip.addEventListener(FlipEvent.PRINT, helpHandler);
			resizeHandler();
			//
			printer_all.addEventListener(MouseEvent.CLICK, allHandler);
			printer_left.addEventListener(MouseEvent.CLICK, leftHandler);
			printer_right.addEventListener(MouseEvent.CLICK, rightHandler);
		}

		private function allHandler(e:MouseEvent):void {
            startPrint(Flip.pages.book);
		}
		private function leftHandler(e:MouseEvent):void {
			if (Flip.pages.pagesL.numChildren) {
            	startPrint(Flip.pages.pagesL); 
			}
		}
		private function rightHandler(e:MouseEvent):void {
			if (Flip.pages.pagesR.numChildren) { 
            	startPrint(Flip.pages.pagesR);
			}
		}
		private function startPrint(sp:Sprite):void {
			try {
				myPrintJob = new PrintJob();
				myPrintJob.start(); 
            	myPrintJob.addPage(sp); 
            	myPrintJob.send(); 
			} catch (e:Error) {
			}
		}
		
		private function buttonHandler(e:MouseEvent):void {
			helpHandler();
		}
		private function headDownHandler(e:MouseEvent):void {
			this.startDrag();
		}
		private function headUpHandler(e:MouseEvent):void {
			this.stopDrag();
		}
		
		private function helpHandler(e:FlipEvent = null):void {
			this.visible = !this.visible;
			this.parent.setChildIndex(this, this.parent.numChildren - 1);
		}

		private function resizeHandler(e:FlipEvent = null):void {
			this.x = (config['width'] - this.width) * 0.5;
			this.y = (config['height'] - this.height) * 0.5;
		}
		
		
	}
}